package com.sat.goods.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sat.common.entity.Result;
import com.sat.goods.model.Spec;
import com.sat.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 相册接口
 *
 * @author long
 */
@RestController
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    @GetMapping
    public Result findAll() {
        List<Spec> list = specService.list();
        return Result.OK(list);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable Integer id) {
        Spec byId = specService.getById(id);
        return Result.OK(byId);
    }

    @PostMapping
    public Result add(@RequestBody Spec spec) {
        boolean save = specService.save(spec);
        if (save) {
            return Result.OK("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @PutMapping
    public Result updata(@RequestBody Spec spec) {
        boolean b = specService.updateById(spec);
        if (b) {
            return Result.OK("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("{id}")
    public Result delete(@PathVariable Integer id) {
        boolean b = specService.removeById(id);
        if (b) {
            return Result.OK("删除成功");
        } else {
            return Result.error("删除失败");
        }
    }

    @PostMapping("/search")
    public Result findList(@RequestBody Spec spec) {
        List<Spec> albums = specService.findList(spec);
        return Result.OK(albums);
    }

    @GetMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable Integer page, @PathVariable Integer size) {
        Page page1 = specService.findPage(page, size);
        return Result.OK(page1);
    }

    @PostMapping("/findPage/{page}/{size}")
    public Result findPage(@PathVariable Integer page, @PathVariable Integer size, @RequestBody Spec spec) {
        Page page1 = specService.findPage(page, size, spec);
        return Result.OK(page1);
    }
}
